package ani.trainning.cacobatph.situation;

import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.TextView;
import ani.trainning.cacobatph.R;

public class AdapterListSituation  extends ArrayAdapter<_Situation> {

	private final Context context;

	public AdapterListSituation(Context context, int textViewResourceId,
			List<_Situation> situations) {
		super(context, textViewResourceId, situations);
		this.layoutInflater=LayoutInflater.from(context);
		this.situations=situations;
		this.context=context;
	}

	List<_Situation>situations=new ArrayList<_Situation>();
	LayoutInflater layoutInflater;
	private Filter _filter = null;

	private final Handler handler = new Handler(){
		@Override
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case 1:
				Bundle bundle = (Bundle)msg.obj;
				((Act8_Situation)context).setValues(bundle.getDouble("credit"), bundle.getDouble("debit"), bundle.getDouble("solde"));
				break;
			}
		};
	};

	@Override
	public int getCount() {
		return this.situations.size();
	}

	@Override
	public _Situation getItem(int arg0) {
		return this.situations.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		return arg0;
	}

	public static  class Holder {
		TextView textPeriode;
		TextView textNature;
		TextView textDebit;
		TextView textCredit;
		TextView textSolde;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		Holder holder;
		if(convertView==null){
			convertView=layoutInflater.inflate(R.layout.situation_item, null);
			holder=new Holder();
			holder.textPeriode=(TextView) convertView.findViewById(R.id.textPeriode);
			holder.textCredit=(TextView) convertView.findViewById(R.id.textItemValueCredit);
			holder.textDebit=(TextView) convertView.findViewById(R.id.textItemValueDabit);
			holder.textSolde=(TextView) convertView.findViewById(R.id.textItemValueSolde);
			holder.textNature=(TextView) convertView.findViewById(R.id.textNature);
			convertView.setTag(holder);
		}
		else{
			holder=(Holder) convertView.getTag();

		}
		DecimalFormat nf = new DecimalFormat("0,000.00" );
		DecimalFormatSymbols s = nf.getDecimalFormatSymbols();
		s.setGroupingSeparator(' ');
		nf.setDecimalFormatSymbols(s);
		holder.textPeriode.setText(situations.get(position).getPeriode());
		holder.textNature.setText(situations.get(position).getNature());
		holder.textCredit.setText(situations.get(position).getCredit()>=1000||situations.get(position).getCredit()<=-1000?""+nf.format(situations.get(position).getCredit()):""+situations.get(position).getCredit());
		holder.textDebit.setText(situations.get(position).getDebit()>=1000||situations.get(position).getDebit()<=-1000?""+nf.format(situations.get(position).getDebit()):""+situations.get(position).getDebit());
		holder.textSolde.setText(situations.get(position).getSolde()>=1000||situations.get(position).getSolde()<=-1000?""+nf.format(situations.get(position).getSolde()):""+situations.get(position).getSolde());
		return convertView;
	}

	@Override
	public Filter getFilter() {
		if(_filter == null)
			_filter = new FilterListSituation(this, situations,context, handler);
		return _filter;
	}


}
